package com.click.app.model;

import com.click.app.Functions;
import com.click.app.util.CurrencyConverter;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

/**
 *
 * @author orgale
 */
@Entity
@Table(name = "storage_materials")
public class StorageMaterial extends BaseObject {

    private Long id;
    private Material material;
    private Double stock;
    private Double limit;
    private List<PackageDetail> packageMaterials = new ArrayList<PackageDetail>();


    public StorageMaterial(){
    }

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Column(name = "id_storage_material", unique = true, nullable = false)
    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name="id_material")
    public Material getMaterial() {
        return this.material;
    }

    public void setMaterial(Material material) {
        this.material = material;
    }

    @Column(name = "stock", precision = 12, scale = 0)
    public Double getStock() {
        return Functions.getDouble3Decimal(this.stock);
    }

    public void setStockFormatted(String stockFormatted){
        System.out.println("stock definido en set: "+getStock());
        CurrencyConverter cc = new CurrencyConverter();
        Double aux = (Double) cc.convert(Double.class, stockFormatted);
        setStock(Double.parseDouble(aux.toString()));
    }

    @Transient
    public String getStockFormatted() {
        System.out.println("stock definido en get: "+getStock());
        CurrencyConverter cc = new CurrencyConverter();
        System.out.println("retornando: "+((String) cc.convert(String.class, (Double.parseDouble(getStock().toString())))));
        return ((String) cc.convert(String.class, (Double.parseDouble(getStock().toString()))));
        }


    public void setStock(Double stock) {
        this.stock = Functions.getDouble3Decimal(stock);
    }

    @Column(name = "stock_limit", precision = 12, scale = 0)
    public Double getLimit() {
        return Functions.getDouble3Decimal(this.limit);
    }

    public void setLimit(Double limit) {
        this.limit = Functions.getDouble3Decimal(limit);
    }

    public void setLimitFormatted(String limitFormatted){
        CurrencyConverter cc = new CurrencyConverter();
        Double aux = (Double) cc.convert(Double.class, limitFormatted);
        setLimit(Double.parseDouble(aux.toString()));
    }

    @Transient
    public String getLimitFormatted() {
        CurrencyConverter cc = new CurrencyConverter();
        return ((String) cc.convert(String.class, (Double.parseDouble(getLimit().toString()))));
        }

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "storageMaterial")
    public List<PackageDetail> getPackageMaterials() {
        if (packageMaterials==null){
            packageMaterials=new ArrayList<PackageDetail>();
        }
        return packageMaterials;
    }

    public void setPackageMaterials(List<PackageDetail> packageMaterials) {
        this.packageMaterials = packageMaterials;
    }

    public void incrementStok( Double quantity ){
        this.stock = Functions.getDouble3Decimal(stock+quantity);
    }

    public void decrementStok( Double quantity ){
        this.stock = Functions.getDouble3Decimal(stock-quantity);
    }

    @Override
    public String toString() {
        return id+","+stock+","+limit;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final StorageMaterial other = (StorageMaterial) obj;
        if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {
            return false;
        }
        if (this.material != other.material && (this.material == null || !this.material.equals(other.material))) {
            return false;
        }
        if (this.stock != other.stock && (this.stock == null || !this.stock.equals(other.stock))) {
            return false;
        }
        if (this.limit != other.limit && (this.limit == null || !this.limit.equals(other.limit))) {
            return false;
        }
        if (this.packageMaterials != other.packageMaterials && (this.packageMaterials == null || !this.packageMaterials.equals(other.packageMaterials))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 67 * hash + (this.id != null ? this.id.hashCode() : 0);
        hash = 67 * hash + (this.material != null ? this.material.hashCode() : 0);
        hash = 67 * hash + (this.stock != null ? this.stock.hashCode() : 0);
        hash = 67 * hash + (this.limit != null ? this.limit.hashCode() : 0);
        hash = 67 * hash + (this.packageMaterials != null ? this.packageMaterials.hashCode() : 0);
        return hash;
    }

    
}
